#ifndef CAMERA_H
#define CAMERA_H

#include <QObject>
#include <opencv2/opencv.hpp>
#include "logger.h"
#include <QThread>
//#include "MvCameraControl.h"

#define CAPTUREHARDWARE_TPYE 1 //0:USB 1:ETH0


class Camera : public QObject {
    Q_OBJECT
    Q_ENUMS(Status)
    Q_PROPERTY(QVariantMap imageInfo READ imageInfo NOTIFY imageInfoChanged)

public:
    Camera(QObject *parent = nullptr);
    ~Camera();
    enum Status {
        Initializing,
        Idle,
        Running,
        Stop,
        Error
    };

    cv::Mat runCaptureOnce();
    QVariantMap imageInfo() const {return m_imageInfo ; }
    QMap<QString, qint64> getStepTimes() const { return stepTimes; }
    void setExposure(int exposure);


public slots:
    void startCapture();
    void stopCapture();
    virtual void configDataChanged(QString Category,QString key);
signals:
    void newFrameAvailable(const cv::Mat& frame);
    void statusChanged(Camera::Status Status);
    void errorOccurred(const QString& errorMessage);
    void frameCaptured(const cv::Mat& frame);
    void imageInfoChanged();

private:

    Camera(const Camera& rhs) = delete;
    Camera& operator=(const Camera& rhs) = delete;
    QVariantMap m_imageInfo;

    Status currentStatus;
    QString lastError;
    void handleCaptureFailure(const std::string &error);
    void sendImgInfo(cv::Mat &frame);
    int CaptureHardwareType;
protected:
    virtual  bool getIsOpened();
    virtual  void capRelease();
    virtual void capOpen();
    virtual cv::Mat getImage();
    QMap<QString, qint64> stepTimes;

    bool recordTime=true;      //是否需要记录并打印每一步时间
    Status getStatus() const {return currentStatus;};
    void setStatus(Status newStatus, QString errorMess = "");
    void logStepTime(const QString &step, qint64 time ,bool recordTime);

};

#endif // CAMERA_H
